package com.aplink.generic;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.aplink.generic.bitmap.ImageFetcher;
import com.aplink.generic.pojo.controlspec.GallerySpec;
import com.aplink.generic.util.ResourcesHelper;

public class PhotoFragment extends Fragment {
    private static final String DEFAULT_IMAGE = "DEFAULT_IMAGE";
    private static final String HEIGHT = "HEIGHT";
    private static final String IMAGE_URL = "IMAGE_URL";
    private static final String MAX_ZOOM = "MAX_ZOOM";
    private static final String MIN_ZOOM = "MIN_ZOOM";
    private static final String NO_PHOTO = "NO_PHOTO";
    private static final String READ_ONLY = "READ_ONLY";
    private static final String WIDTH = "WIDTH";

    public static PhotoFragment newInstance(final String imageURL,
            final GallerySpec gallerySpec, final ImageFetcher imageFetcher) {
        final PhotoFragment fragment = new PhotoFragment();
        fragment.mImageFetcher = imageFetcher;
        final Bundle bundle = new Bundle();
        bundle.putString(PhotoFragment.IMAGE_URL, imageURL);
        bundle.putString(PhotoFragment.DEFAULT_IMAGE,
                gallerySpec.getDefaultImage());
        bundle.putString(PhotoFragment.NO_PHOTO, gallerySpec.getNoPhoto());
        bundle.putBoolean(PhotoFragment.READ_ONLY, gallerySpec.isReadOnly());
        bundle.putInt(PhotoFragment.WIDTH, gallerySpec.getImageWidth());
        bundle.putInt(PhotoFragment.HEIGHT, gallerySpec.getImageHeight());
        bundle.putInt(PhotoFragment.HEIGHT, gallerySpec.getImageHeight());
        bundle.putFloat(PhotoFragment.MIN_ZOOM, gallerySpec.getMinZoom());
        bundle.putFloat(PhotoFragment.MAX_ZOOM, gallerySpec.getMaxZoom());

        fragment.setArguments(bundle);
        return fragment;
    }

    private ImageFetcher mImageFetcher;

    private ImageView mImageView;

    private ProgressBar mProgressBar;

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final Bundle bundle = getArguments();
        final String imageURL = bundle.getString(PhotoFragment.IMAGE_URL);
        final String noPhoto = bundle.getString(PhotoFragment.NO_PHOTO);
        this.mImageFetcher.setReadOnly(bundle
                .getBoolean(PhotoFragment.READ_ONLY));
        this.mImageFetcher.setMaxZoom(bundle.getFloat(PhotoFragment.MAX_ZOOM));
        this.mImageFetcher.setMinZoom(bundle.getFloat(PhotoFragment.MIN_ZOOM));
        if (!TextUtils.isEmpty(bundle.getString(PhotoFragment.DEFAULT_IMAGE))) {
            final int resId = ResourcesHelper.getDrawableIdByName(bundle
                    .getString(PhotoFragment.DEFAULT_IMAGE));
            this.mImageFetcher.setLoadingImage(resId);
        }
        if (TextUtils.isEmpty(imageURL)) {
            this.mProgressBar.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(noPhoto)) {
                final int resId = ResourcesHelper.getDrawableIdByName(noPhoto);
                final Drawable drawable = getActivity().getResources()
                        .getDrawable(resId);
                this.mImageView.setImageDrawable(drawable);
            }
        } else {
            this.mImageFetcher.loadImage(imageURL, this.mImageView);
        }
    }

    @Override
    public View onCreateView(final LayoutInflater inflater,
            final ViewGroup container, final Bundle savedInstanceState) {
        final FrameLayout frameLayout = new FrameLayout(getActivity()
                .getApplicationContext());
        final FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);
        frameLayout.setLayoutParams(frameLayoutParams);

        final FrameLayout.LayoutParams progressBarParams = new FrameLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

        this.mProgressBar = new ProgressBar(getActivity());
        progressBarParams.gravity = Gravity.CENTER;
        this.mProgressBar.setLayoutParams(progressBarParams);

        final FrameLayout.LayoutParams imageViewParams = new FrameLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);
        this.mImageView = new ImageView(getActivity());
        this.mImageView.setLayoutParams(imageViewParams);

        frameLayout.addView(this.mProgressBar);
        frameLayout.addView(this.mImageView);
        return frameLayout;
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}
